using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace WIT.DAQ.Assistant;

internal static class LocalLog
{
	private static readonly string ROOT = AppDomain.CurrentDomain.BaseDirectory;

	private static readonly string FOLDER = "log";

	internal static void Log(string content, string fileNamePrefix = "log")
	{
		try
		{
			string text = ROOT + FOLDER + "\\" + DateTime.Now.ToString("yyyyMMdd");
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			StreamWriter streamWriter = new StreamWriter(text + "\\" + fileNamePrefix + DateTime.Now.ToString("yyyyMMdd") + ".log", append: true);
			streamWriter.WriteLine(" ----- " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ----- ");
			streamWriter.WriteLine(content);
			streamWriter.Flush();
			streamWriter.Close();
		}
		catch
		{
		}
	}

	internal static void Error(string content)
	{
		try
		{
			string text = ROOT + FOLDER + "\\" + DateTime.Now.ToString("yyyyMMdd");
			if (!Directory.Exists(text))
			{
				Directory.CreateDirectory(text);
			}
			string path = text + "\\exception" + DateTime.Now.ToString("yyyyMMdd") + ".log";
			MethodBase method = new StackTrace(fNeedFileInfo: true).GetFrame(1).GetMethod();
			StreamWriter streamWriter = new StreamWriter(path, append: true);
			streamWriter.WriteLine(" ----- " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " -----  " + method.DeclaringType.FullName + "." + method.Name);
			streamWriter.WriteLine(content);
			streamWriter.Flush();
			streamWriter.Close();
		}
		catch
		{
		}
	}
}
